package com.xiaodou.mjf.web.console.core.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaodou.mjf.application.core.manager.AccountManager;
import com.xiaodou.mjf.application.core.manager.OrderManager;
import com.xiaodou.mjf.application.core.service.DepositService;
import com.xiaodou.mjf.application.core.service.OrderService;
import com.xiaodou.mjf.application.core.service.WithdrawalService;
import com.xiaodou.mjf.entity.core.Deposit;
import com.xiaodou.mjf.entity.core.Order;
import com.xiaodou.mjf.entity.core.Withdrawal;
import com.xiaodou.mjf.infrastructure.common.components.Result;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;

/**
 * 订单管理
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("core/order")
public class OrderController extends ApplicationController {

	@Resource
	DepositService depositService;
	@Resource
	WithdrawalService withdrawalService;
	@Resource
	OrderService orderService;
	@Resource
	AccountManager accountManager;
	@Resource
	OrderManager orderManager;

	@RequestMapping("withdrawal")
	public String withdrawalList(Withdrawal withdrawal) {

		return "core/order/withdrawal_list";
	}

	@RequestMapping("deposit")
	public String depositList(Deposit deposit, Model model) {

		return "core/order/deposit_list";
	}

	@RequestMapping("payment")
	public String paymentList(Order order, Model model) {

		return "core/order/paymentOrder_list";
	}

	@RequestMapping(value = { "deposit/depData.json" })
	@ResponseBody
	public Map<String, Object> depData(Deposit deposit) {
		Page<Deposit> page = depositService.page(deposit);
		return page(page);
	}

	@RequestMapping(value = { "withdrawal/witData.json" })
	@ResponseBody
	public Map<String, Object> witData(Withdrawal withdrawal) {
		Page<Withdrawal> page = withdrawalService.page(withdrawal);
		return page(page);
	}

	@RequestMapping(value = { "payment/paymentData.json" })
	@ResponseBody
	public Map<String, Object> paymetnData(Order order) {
		Page<Order> page = orderService.page(order);
		return page(page);
	}

	@RequestMapping(value = { "audit", "audit/{id}" })
	@ResponseBody
	public Map<String, Object> audit(@PathVariable String id, Integer audtiStatus, String content, Model model) throws Exception {

		Result result = new Result();
		try {
			Withdrawal withdrawal = withdrawalService.get(id);
			if (withdrawal != null) {
				withdrawal.setStatus(audtiStatus);
				withdrawal.setAuditContent(content);
				result = orderManager.audit(withdrawal);

			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}
		return map(result.isSuccess(), result.getMessage());
	}
}
